package cn.yqx.bankshop.goods.config;

import cn.yqx.bankshop.constants.CommonConstant;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.io.IOException;

@Configuration
public class TokenConfig {
    private final static String  PUBLIC_KEY_FILE = "public.txt" ;
    @Autowired
    RedisConnectionFactory redisConnectionFactory ;

    @Bean
    public TokenStore getTokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        ClassPathResource classPathResource = new ClassPathResource(PUBLIC_KEY_FILE);
        String publicKey = null ;
        try{
            publicKey = IOUtils.toString(classPathResource.getInputStream(), CommonConstant.UTF8) ;

        }catch (IOException e){

        }
        converter.setVerifierKey(publicKey);
        return converter;
    }
}
